#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

vector<vector<int>> threeNumberSum(vector<int> &arr, int targetSum) {
  sort(arr.begin(), arr.end());
  vector<vector<int>> result;

  for (int i = 0; i < arr.size(); ++i) {
    int n = arr[i];
    int remain = targetSum - n;

    int l = i + 1;
    int r = arr.size() - 1;

    while (l < r) {
      int sum = n + arr[l] + arr[r];

      if (sum == targetSum) {
        result.push_back({n, arr[l], arr[r]});
        break;
      }
      else if (sum < targetSum) {
        ++l;
      }
      else {
        --r;
      }
    }
  }

  return result;
}

int main(int argc, char const *argv[])
{
  /* code */
  vector<int> test{
    12, 3, 1, 2, -6, 5, -8, 6
  };

  auto result = threeNumberSum(test, 0);

  for (vector<int> v : result) {
    for (int n : v) {
      cout << n << " ";
    }

    cout << endl;
  }

  return 0;
}
